package com.imooc.demo.web;

import com.imooc.demo.dao.ShouCangDao;
import com.imooc.demo.entity.ShouCang;
import com.imooc.demo.entity.Talk;
import com.imooc.demo.service.ShouCangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoucang")
public class ShouCangController {
    @Autowired
    ShouCangService shouCangService;

    ///////创建留言
    @PostMapping("/insertShouCang")
    public Map<String, Object> insertShouCang(@RequestParam("buyerOpenid") String buyerOpenid,
                                              @RequestParam("talkId") Integer talkId) {

        System.out.println("进来了");
        ShouCang shouCang = new ShouCang();
        shouCang.setBuyerOpenid(buyerOpenid);
        shouCang.setTalkId(talkId);

        System.out.println("shouCang:"+shouCang.toString());
        // 添加到数据库
        Boolean flag = shouCangService.insertShouCang(shouCang);
        if(!flag){
            System.out.println("插入数据库shouCang失败！");
        }else {
            System.out.println("插入数据库shouCang成功！");
        }
        // 封装返回小程序
        Map<String, Object> result = new HashMap<>();
        result.put("success", flag);
        return  result;
    }


    //通过buyerOpenid获取留言信息
    @RequestMapping(value = "getShouCangList",method = RequestMethod.GET)
    private  Map<String,Object>  getShouCangList( @RequestParam("buyerOpenid") String buyerOpenid
    ){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<ShouCang> shouCangList= shouCangService.getShouCangList(buyerOpenid);

        System.out.println("shouCangList:"+shouCangList.toString());
        modelMap.put("shouCangList",shouCangList);
        return modelMap;
    }


    //用户删除
    @RequestMapping(value = "/deleteShouCang",method = RequestMethod.GET)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
    private  Map<String,Object>  deleteShouCang(@RequestParam("talkId") Integer talkId,
                                                @RequestParam("buyerOpenid") String buyerOpenid){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        ShouCang shouCang= new ShouCang();

        shouCang.setTalkId(talkId);
        shouCang.setBuyerOpenid(buyerOpenid);
        ShouCang shouCang0= shouCangService.getShouCang(shouCang);
        modelMap.put("success",shouCangService.deleteShouCang(shouCang0.getId()));
        return modelMap;
    }

   //所有Talk
    @RequestMapping(value = "getShouCang",method = RequestMethod.GET)
    private  Map<String,Object>  getShouCang(@RequestParam("talkId") Integer talkId,
                                             @RequestParam("buyerOpenid") String buyerOpenid){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        ShouCang shouCang= new ShouCang();
        //shouCang.setId(id);
        shouCang.setTalkId(talkId);
        shouCang.setBuyerOpenid(buyerOpenid);
        ShouCang shouCang0= shouCangService.getShouCang(shouCang);
            System.out.println(shouCang0);
            if(shouCangService.getShouCang(shouCang)==null){
                modelMap.put("shouCang",false);
            }else {

        System.out.println("shouCang0:"+shouCang0.toString());
        modelMap.put("shouCang",shouCang0);}
        return modelMap;
    }


    //所有Talk
   /*@RequestMapping(value = "getAllShouCangList",method = RequestMethod.GET)
    private  Map<String,Object>  allTalkList(){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<ShouCang> shouCangList= shouCangService.getAllShouCangList();

        System.out.println("shouCangList:"+shouCangList.toString());
        modelMap.put("shouCangList",shouCangList);
        return modelMap;
    }*/

}
